#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# 创建并进入构建目录
mkdir -p build
cd build

# 检查CMake是否安装
if ! command -v cmake &> /dev/null; then
    echo -e "${RED}Error: CMake is not installed${NC}"
    exit 1
fi

# 检查Qt是否安装
if ! command -v qmake &> /dev/null; then
    echo -e "${RED}Error: Qt is not installed${NC}"
    exit 1
fi

# 构建类型
BUILD_TYPE=${1:-Release}  # 默认Release构建
echo -e "${GREEN}Building with type: ${BUILD_TYPE}${NC}"

# 运行CMake配置
cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ..
if [ $? -ne 0 ]; then
    echo -e "${RED}CMake configuration failed${NC}"
    exit 1
fi

# 编译
cmake --build . --config ${BUILD_TYPE} -j$(nproc)
if [ $? -ne 0 ]; then
    echo -e "${RED}Build failed${NC}"
    exit 1
fi

# # 运行测试
# echo -e "${GREEN}Running tests...${NC}"
# ctest --output-on-failure
# if [ $? -ne 0 ]; then
#     echo -e "${RED}Tests failed${NC}"
#     e
echo -e "${GREEN}Build completed successfully${NC}" 